"use strict";
const common_vendor = require("../../common/vendor.js");
const common_assets = require("../../common/assets.js");
const utils_api = require("../../utils/api.js");
const _sfc_main = {
  __name: "ai-dialog",
  setup(__props) {
    const inputMessage = common_vendor.ref("");
    const messages = common_vendor.ref([
      {
        type: "ai",
        content: "你好！我是你的 AI 助手，有什么可以帮你的吗？"
      }
    ]);
    const scrollTop = common_vendor.ref(0);
    const loading = common_vendor.ref(false);
    const scrollHeight = common_vendor.ref(0);
    const calculateScrollViewHeight = () => {
      const systemInfo = common_vendor.index.getSystemInfoSync();
      const windowHeight = systemInfo.windowHeight;
      const inputAreaHeight = 120;
      scrollHeight.value = windowHeight - inputAreaHeight;
    };
    common_vendor.onMounted(() => {
      calculateScrollViewHeight();
    });
    const sendMessage = async () => {
      if (!inputMessage.value.trim() || loading.value)
        return;
      const userMessage = inputMessage.value.trim();
      messages.value.push({
        type: "user",
        content: userMessage,
        time: (/* @__PURE__ */ new Date()).toLocaleTimeString()
      });
      inputMessage.value = "";
      await common_vendor.nextTick$1();
      scrollToBottom();
      loading.value = true;
      try {
        const aiResponse = await utils_api.courseApi.chatWithAI(userMessage);
        console.log("AI 响应:", aiResponse);
        messages.value.push({
          type: "ai",
          content: aiResponse.data.aimessage,
          time: (/* @__PURE__ */ new Date()).toLocaleTimeString()
        });
        await common_vendor.nextTick$1();
        scrollToBottom();
      } catch (error) {
        common_vendor.index.showToast({
          title: error.message || "发送失败，请重试",
          icon: "none"
        });
      } finally {
        loading.value = false;
      }
    };
    const scrollToBottom = () => {
      setTimeout(() => {
        scrollTop.value = 9999999;
      }, 100);
    };
    const loadMore = () => {
      console.log("load more");
    };
    return (_ctx, _cache) => {
      return common_vendor.e({
        a: common_vendor.f(messages.value, (msg, index, i0) => {
          return {
            a: common_vendor.t(msg.time || "刚刚"),
            b: `/static/icons/${msg.type}.png`,
            c: common_vendor.t(msg.content),
            d: common_vendor.n(msg.type),
            e: index
          };
        }),
        b: loading.value
      }, loading.value ? {
        c: common_assets._imports_0
      } : {}, {
        d: scrollTop.value,
        e: common_vendor.o(loadMore),
        f: scrollHeight.value + "px",
        g: common_vendor.o(sendMessage),
        h: inputMessage.value,
        i: common_vendor.o(($event) => inputMessage.value = $event.detail.value),
        j: common_vendor.o(sendMessage),
        k: !inputMessage.value.trim()
      });
    };
  }
};
const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["__scopeId", "data-v-15ea54df"]]);
wx.createPage(MiniProgramPage);
